<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">账户编号</label> -->
        <!-- <el-input v-model="query.accountId" clearable placeholder="账户编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <label class="el-form-item-label">用户名</label>
        <el-input v-model="query.customerName" clearable placeholder="用户名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <!-- <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          style="height: 29px; margin-right: 15px;"
          @click="detail()"
        >兑换</el-button> -->
      </crudOperation>

      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="90px">
          <!-- <el-form-item v-if="false" label="编号" prop="id">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="账户编号">
            <el-input v-model.trim="form.accountId" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="用户名">
            <el-input v-model.trim="form.customerName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="累计总积分">
            <el-input v-model="form.totalIntegral" type="number" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="兑换中积分">
            <el-input v-model="form.exchangeIntegral" type="number" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="已提取积分">
            <el-input v-model="form.consumeIntegral" type="number" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="积分余额">
            <el-input v-model="form.remainIntegral" type="number" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="积分等级">
            <el-select v-model="form.integralGrade" filterable placeholder="请选择" style="width: 370px;">
              <el-option
                v-for="item in dict.integral_grade"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="账户状态">
            <el-select v-model="form.integralStatus" filterable placeholder="请选择" style="width: 370px;">
              <el-option
                v-for="item in dict.integral_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker v-model="form.updateTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model.trim="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column v-if="false" prop="id" label="编号" /> -->
        <!-- <el-table-column prop="accountId" label="账户编号" align="center" /> -->
        <el-table-column prop="customerName" label="用户名" align="center" />
        <el-table-column label="累计总积分" align="center">
          <template slot-scope="scope">
            {{ (scope.row.totalIntegral/100).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="兑换中积分" align="center">
          <template slot-scope="scope">
            {{ (scope.row.exchangeIntegral/100).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="已提取积分" align="center">
          <template slot-scope="scope">
            {{ (scope.row.consumeIntegral/100).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="积分余额" align="center">
          <template slot-scope="scope">
            {{ (scope.row.remainIntegral/100).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="integralGrade" label="积分等级" align="center">
          <template slot-scope="scope">
            {{ dict.label.integral_grade[scope.row.integralGrade] }}
          </template>
        </el-table-column>
        <el-table-column prop="integralStatus" label="账户状态" align="center">
          <template slot-scope="scope">
            {{ dict.label.integral_status[scope.row.integralStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label=" 更新时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="remark" label="备注" align="center" /> -->
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button type="warn" :disabled="Number(scope.row.accountId) === user.id?false:true" @click="detail(scope.row, 1)">兑换</el-button>
            <el-button v-permission="['admin','integral:integralDetail']" type="primary" @click="detail(scope.row, 2)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudIntegral from '@/api/integral/integral'
import { extractApply } from '@/api/integral/integral'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { Notification } from 'element-ui'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, accountId: null, customerName: null, totalIntegral: null, exchangeIntegral: null, consumeIntegral: null, remainIntegral: null, integralGrade: null, integralStatus: null, createTime: null, updateTime: null, remark: null }
export default {
  name: 'Integral',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['integral_grade', 'integral_status'],
  cruds() {
    return CRUD({ title: 'integral', url: 'api/integral', idField: 'id', sort: 'id,desc', crudMethod: { ...crudIntegral },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true
      }
    })
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  data() {
    return {
      permission: {
        add: ['admin', 'integral:add'],
        edit: ['admin', 'integral:edit'],
        del: ['admin', 'integral:del']
      },
      rules: {
        id: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'accountId', display_name: '账户编号' },
        { key: 'customerName', display_name: '账户姓名' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      form.integralStatus = form.integralStatus.toString()
      form.integralGrade = form.integralGrade.toString()
    },
    detail(value, index) {
      if (index === 1) {
        this.$confirm('确认积分兑换吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          extractApply({ 'id': value.id, 'remainIntegral': (value.remainIntegral / 100).toFixed(2) }).then(res => {
            Notification.success('申请提交成功')
          })
        }).catch(() => {

        })
      } else {
        this.$router.push({ path: '/integral/integralDetail', query: { accountId: value.accountId }})
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .head-container {
    /** 去除input输入框样式 */
    ::v-deep  input[type=number]::-webkit-inner-spin-button {
      -webkit-appearance: none;
    }
	::v-deep input[type="number"]{
	    -moz-appearance:textfield;
	}
  }
</style>
